//
//  DateHelper.m
//  BZBN
//
//  Created by chen on 2017/8/21.
//  Copyright © 2017年 KuoShuChing. All rights reserved.
//

#import "DateHelper.h"

@implementation DateHelper
+ (NSString *)getNowDateStr {
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *nowdate = [NSDate date];
    NSString *dateStr = [dateformater stringFromDate:nowdate];
    return dateStr;
}

+ (NSString *)getDateStrFromDate:(NSDate *)date {
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [dateformater stringFromDate:date];
    return dateStr;
}

+ (NSDate *)getDteFromDateStr:(NSString *)dateStr {
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformater dateFromString:dateStr];
    return date;
}

+ (NSString *)dateToDatesting:(NSString*)timeStr{
    //时间转时间戳
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:timeStr];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    return timeSp;
}
//将时间戳转化成时间
+(NSString *)timestampToTimeStr:(NSString *)timestamp Dateformatter:(NSString *)dateformatter
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateformatter];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp.doubleValue];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    return dateStr;
}

+ (NSString *)dateToDatesting:(NSString*)timeStr Formatter:(NSString *)formatter{
    //时间转时间戳
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    NSDate *date = [dateFormatter dateFromString:timeStr];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    return timeSp;
}

/**
 * 计算指定时间与当前的时间差
 * @param timeString   某一指定时间的时间戳
 * @return 多少(秒or分or天or月or年)+前 (比如，3天前、10分钟前)
 */
+(NSString *)getTimeWithTimeIntervalString:(NSString *)timeStampString{
    
    //生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue] / 1000.0;
    NSDate *date1               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *timeString       = [formatter1 stringFromDate: date1];
    
    // 格式化时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:timeString];
    
    NSTimeInterval  timeInterval = [date timeIntervalSinceNow];
    timeInterval =  [[NSDate date] timeIntervalSinceNow] - timeInterval;
    int temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%d分钟前",temp];
    }else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%d小时前",temp];
    }else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%d天前",temp];
    }else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%d月前",temp];
    }else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%d年前",temp];
    }
    return result;
}

@end
